﻿using NHibernate;
using System.Collections.Generic;
using NHibernate.Criterion;
using Inovout.Models;

namespace Inovout.Repositories
{
    public class CriteriaQuery<TModel> where TModel : class
    {
        private ICriteria nhibernateCriteria;

        public CriteriaQuery(ICriteria nhibernateCriteria)
        {
            this.nhibernateCriteria = nhibernateCriteria;
        }

        public IList<TModel> Execute()
        {
            return nhibernateCriteria.List<TModel>();
        }
        internal CriteriaQuery<TModel> Where(params ICriterion[] criterions)
        {
            foreach (ICriterion criterion in criterions)
            {
                nhibernateCriteria.Add(criterion);
            }
            return this;
        }
        internal CriteriaQuery<TModel> Top(int top)
        {
            nhibernateCriteria.SetMaxResults(top);
            return this;
        }
        internal CriteriaQuery<TModel> OrderBy(params Order[] orders)
        {
            foreach (Order order in orders)
            {
                nhibernateCriteria.AddOrder(order);
            }
            return this;
        }
    }
}
